﻿<#
MIT License
Copyright (c) 2016 powershell-kitchen.com/ PS Cheff
http://www.powershell-kitchen.com/page/mit-license
Published on: http://powershell-kitchen.com/post/protecting-your-secrets
#>

<#
 
.SYNOPSIS
 This script encrypts a string to only be decripted in the curernt machine. 
 
.DESCRIPTION
 This script encrypts a string to only be decripted in the current machine. In addition it could be mapped also to teh current user 
 
.EXAMPLE
 .\protectString.ps1 "My Secret String"
 
.NOTES
 

#>
 
Param(
  [Parameter(Mandatory=$True,Position=1)]
  [string]$value,

  [switch] $userContext
)
[void] [Reflection.Assembly]::LoadWithPartialName("System.Security") 

$secretBytes = [System.Text.UTF8Encoding]::UTF8.GetBytes($value)
if ($userContext)
{
    $result = [System.Security.Cryptography.ProtectedData]::Protect($secretBytes,$null,[System.Security.Cryptography.DataProtectionScope]::CurrentUser)  
}
else
{
    $result = [System.Security.Cryptography.ProtectedData]::Protect($secretBytes,$null,[System.Security.Cryptography.DataProtectionScope]::LocalMachine)  
}
[System.Convert]::ToBase64String($result)